package ${daoPackage};


import com.neuedu.his.common.utils.DBUtils;
import ${entityPackage}.${className};

import java.util.List;
import java.util.Map;

public class ${className}Dao {

    /**
    * 查询集合
    */
    public List<${className}> selectList(String where , Object ... params){
        String sql =" select * from ${tableName} ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<${className}> ${className?uncap_first}s = DBUtils.selectList(${className}.class, sql, params);

        return ${className?uncap_first}s;
     }

    /**
    * 分页查询
    * @param where
    * @param pageNo
    * @param size
    * @param params
    * @return
    */
    public List<${className}> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from ${tableName} ";
        if(where != null && !"".equals(where)){
        sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<${className}> ${className?uncap_first}s = DBUtils.selectList(${className}.class, sql, params);

        return ${className?uncap_first}s;
    }


    /**
    * 查询单条结果
    */
    public ${className} selectOne(String where ,Object ... params){
        String sql =" select * from ${tableName} ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(${className}.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from ${tableName} ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
    * 根据数据进行插入
    * @param ${className?uncap_first}
    * @return
    */
    public int insert(${className} ${className?uncap_first}){
        String sql = "insert into ${tableName}(   <#list insertProps  as prop>    ${prop.columnName}<#if prop_index != insertProps?size -1 >   ,  </#if> </#list>  ) values( <#list insertProps  as prop>  ?<#if prop_index != insertProps?size -1 >   ,  </#if> </#list>   )";
        return DBUtils.update( sql, <#list insertProps  as prop>   ${className?uncap_first}.get${prop.name?cap_first}() <#if prop_index != insertProps?size -1 >   ,  </#if></#list>);
    }


    /**
    * 根据主键更新一条
    * @param ${className?uncap_first}
    * @return
    */
    public int updateById(${className} ${className?uncap_first}){
        String sql = " update ${tableName} set <#list updateProp  as prop>    ${prop.columnName} = ?<#if prop_index != updateProp?size -1 >   ,  </#if> </#list>  where  ${pkProp.columnName} = ?  ";

        return DBUtils.update( sql,
                <#list updateProp  as prop>
                    ${className?uncap_first}.get${prop.name?cap_first}()  ,
                </#list>
                    ${className?uncap_first}.get${pkProp.name?cap_first}() );
    }



    /**
    * 根据主键更新一条
    * @param id
    * @return
    */
    public int deleteById(Integer id){
        String sql = " delete from ${tableName}  where ${pkProp.columnName} = ?";
        return DBUtils.update( sql, id);

    }


}